Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

377
Vistas
Service worker in NX/React workspace can"t be register because of "import"

I have a workspace in react where I am trying to setup service workers.

in main.tsx I do :

import * as serviceWorkerRegistration from './serviceWorkerRegistration';

serviceWorkerRegistration.register({
  onSuccess: () => pwaStore.setState((state) => ({ ...state, isReady: true })),
  onUpdate: () => pwaStore.setState((state) => ({ ...state, hasUpdate: true })),
});

serviceWorker.js is the same as the default one from pwa-template, I just changed this line to work with NX.

      const swUrl = `${process.env.NX_PUBLIC_URL}/service-worker.js`;
      registerValidSW(swUrl, config);

service-worker.js is the default one and contains some imports

import { clientsClaim } from 'workbox-core';
import { ExpirationPlugin } from 'workbox-expiration';
import { precacheAndRoute, createHandlerBoundToURL } from 'workbox-precaching';
import { registerRoute } from 'workbox-routing';
import { StaleWhileRevalidate } from 'workbox-strategies';

Now, when I deploy and run this project, I am prompted with

Error during service worker registration: DOMException: Failed to register a ServiceWorker for scope ('xxx') with script ('xxx/service-worker.js'): The script has an unsupported MIME type ('text/html').

So, I guess the service-worker is not found when loading from direct url. So I added it on the build

        "assets": [
          "apps/core/src/service-worker.js",
          "apps/core/src/manifest.json",
          "apps/core/src/robots.txt",
          "apps/core/src/favicon.ico",
          "apps/core/src/assets"
        ],

and now, it does find it, but I get prompted with

Uncaught SyntaxError: Cannot use import statement outside a module (at service-worker.js:1:1)

I am unsure on how to fix this now.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda